<template>
  <div class="mod-config">
    <el-form
      size="mini"
      :inline="true"
      @keyup.enter.native="getDataList(true)"
    >
      <el-form-item>
        <el-input
          v-model="queryParams.queryModel.projectName"
          clearable
          placeholder="项目名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="getDataList(true)"
        >
          查询
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-delete"
          @click="resetParams"
        >
          重置
        </el-button>
        <el-button
          v-if="isAuth('sport:prescriptionexceptions:save')"
          type="primary"
          icon="el-icon-plus"
          @click="addHandle()"
        >
          新增
        </el-button>
        <el-button
          v-if="isAuth('sport:prescriptionexceptions:save')"
          type="primary"
          :loading="btnLoading"
          icon="el-icon-upload2"
          @click="excelExport()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="table"
      v-loading="dataListLoading"
      highlight-current-row
      border
      :header-cell-style="{background:'#ECF5FF',color:'#000',fontWeight:'bold',fontSize:'14px'}"
      :height="tableHeight"
      :data="pageData.list"
      :default-sort="{prop: 'updateTime', order: 'ascending'}"
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column
        sortable
        prop="projectName"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        min-width="150"
        label="项目名称"
      />
      <el-table-column
        sortable
        prop="exceptionsInstructions"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        min-width="150"
        label="免责申明"
      />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        min-width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sport:prescriptionexceptions:update')"
            type="text"
            style="padding:0;"
            @click="updateHandle(scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-if="isAuth('sport:prescriptionexceptions:update')"
            type="text"
            style="padding:0;"
            @click="copyHandle(scope.row)"
          >
            拷贝
          </el-button>
          <el-button
            v-if="isAuth('sport:prescriptionexceptions:delete')"
            type="text"
            style="padding:0;"
            @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryParams.pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      :total="pageData.totalCount"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :type="dialog.type"
      @close="editClose"
      @success="editSuccess"
    />
  </div>
</template>

<script>
  import edit from './prescriptionexceptions-edit'
  import { initQueryParams } from '@/utils'
  import { dateSelectShorts} from '@/utils/common'

  export default {
    components: { edit },
    data () {
      return {
        dialog: {
          isVisible: false,
          type: 'add'
        },
        tableHeight: this.$store.state.common.documentClientHeight - 260,
        queryParams: initQueryParams(),
        pageData: {
          totalCount: 0,
          list: [],
          extData: {}
        },
        dataListLoading: false,
        btnLoading: false,
        createTimePickerOptions: {
          shortcuts: dateSelectShorts()
        }
      }
    },
    activated () {
      this.getDataList(true)
    },
    updated () {
      this.$nextTick(() => {
        this.$refs['table'].doLayout()
      })
    },
    methods: {
      editClose() {
        this.dialog.isVisible = false
      },
      editSuccess() {
        this.getDataList(true)
      },
      resetParams() {
        this.queryParams = initQueryParams()
        this.getDataList(true)
      },
      // 获取数据列表
      getDataList (isReload) {
        if (isReload) {
          this.queryParams.pageNo = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.requestUrl('/sport/prescriptionexceptions/list'),
          method: 'get',
          params: this.$http.requestParams(this.queryParams)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.pageData = data.page
          }
          this.dataListLoading = false
        })
      },
      // 导出excel
      excelExport () {
        this.btnLoading = true
        this.$http({
          url: this.$http.requestUrl('/sport/prescriptionexceptions/exportExcel'),
          method: 'get',
          responseType: 'arraybuffer',
          params: this.$http.requestParams(this.queryParams)
        }).then(({data}) => {
          this.btnLoading = false
          let blob = new Blob([data])
          let fileName = '导出运动处方免责事项说明.xlsx'
          // for IE
          if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(blob, fileName)
          } else { // for Non-IE (chrome, firefox ...)
            let downloadElement = document.createElement('a')
            let href = window.URL.createObjectURL(blob)
            downloadElement.href = href
            downloadElement.download = fileName
            document.body.appendChild(downloadElement)
            downloadElement.click()
            document.body.removeChild(downloadElement)
            window.URL.revokeObjectURL(href)
          }
        })
      },
      // 时间选择change事件
      dateChange (val) {
        this.queryParams.queryModel.createTime = val == null ? '' : val[0]
        this.queryParams.queryModel.createTimeEnd = val == null ? '' : val[1]
      },
      // 每页数
      sizeChangeHandle (val) {
        this.queryParams.pageSize = val
        this.queryParams.pageNo = 1
        this.getDataList(false)
      },
      // 当前页
      currentChangeHandle (val) {
        this.queryParams.pageNo = val
        this.getDataList(false)
      },
      // 排序
      sortChange: function (column) {
        if (column.order === 'descending') {
          this.queryParams.sort = column.prop + ' desc'
        } else if (column.order === 'ascending') {
          this.queryParams.sort = column.prop + ' asc'
        }
        this.getDataList(true)
      },
      // 新增
      addHandle () {
        this.dialog.type = 'add'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(false)
      },
      // 修改
      updateHandle (row) {
        this.dialog.type = 'update'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(row)
      },
      // 拷贝
      copyHandle (row) {
        this.dialog.type = 'copy'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(row)
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对id=${id}的记录进行删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.requestUrl('/sport/prescriptionexceptions/delete'),
            method: 'post',
            data: id
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(true)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
